// CreateZip.cs
// 
// ----------------------------------------------------------------------
// Copyright (c) 2006, 2007 Microsoft Corporation.  All rights reserved.
//
// This example is released under the Microsoft Permissive License of
// October 2006.  See the license.txt file accompanying this release for 
// full details. 
//
// ----------------------------------------------------------------------
//
// This simplistic utility zips/compresses a single file.
//
// compile with:
//     csc /debug+ /target:exe /out:CreateZip.exe CreateZip.cs Zip.cs Crc32.cs
//
// Wed, 29 Mar 2006  14:36
//

using System;
using System.IO;

public class Program
{

  private static void Usage() 
  {
      Console.WriteLine("usage:\n  ZipFolder <SourceFolder> <ZipFileToCreate>");
    Environment.Exit(1);
  }

  public static void Main(String[] args) 
  {
    if (args.Length != 2) Usage();
    if (!System.IO.Directory.Exists(args[0])) {
      Console.WriteLine("The source folder does not exist!\n");
      Usage();
    }
    if (System.IO.File.Exists(args[1])) {
      Console.WriteLine("That zipfile already exists!\n");
      Usage();
    }
    if (!args[1].EndsWith(".zip")) {
      Console.WriteLine("The filename must end with .zip!\n");
      Usage();
    }

    try 
    {
      using(ZipFile zip= new ZipFile(args[1]))
      {
          //add files in root of directory...
          string[] rootFileNames = Directory.GetFiles(args[0]);
          foreach (string fileName in rootFileNames)
          {
              if (((File.GetAttributes(fileName) & FileAttributes.Hidden) != FileAttributes.Hidden) &&
              ((File.GetAttributes(fileName) & FileAttributes.System) != FileAttributes.System))
              {
                  //string filename = args[0].ToString().Trim();
                  Console.WriteLine("Adding {0}...", fileName);
                  zip.AddFile(fileName);
              }
          }

          //now all all subdirectories and files within them
          string[] directoryNames = Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories);
          foreach (string directoryName in directoryNames)
          {
              DirectoryInfo di = new DirectoryInfo(directoryName);
              if ((di.Attributes & FileAttributes.Directory) == FileAttributes.Directory
                  && (di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
              {
                  //zip.AddDirectory(directoryName);
                  string[] fileNames = Directory.GetFiles(directoryName);
                  foreach (string fileName in fileNames)
                  {
                      if (((File.GetAttributes(fileName) & FileAttributes.Hidden) != FileAttributes.Hidden) &&
                        ((File.GetAttributes(fileName) & FileAttributes.System) != FileAttributes.System))
                      {
                          //string filename = args[0].ToString().Trim();
                          Console.WriteLine("Adding {0}...", fileName);
                          zip.AddFile(fileName);
                      }
                  }
              }
          }
          zip.Save(); 
      }

    }
    catch (System.Exception ex1)
    {
      System.Console.Error.WriteLine("exception: " + ex1);
    } 

  }

}
